iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 29
0
DevOps

從開源雲到邊緣運算系列 第 29

[Day 29] Openvino模型應用

  • 分享至 

  • xImage
  •  

這次將從tensorflow的retrain model測試,首先先透過官方的flower資料集訓練

cd ~
curl -LO http://download.tensorflow.org/example_images/flower_photos.tgz
tar xzf flower_photos.tgz
mkdir ~/example_code
cd ~/example_code
curl -LO https://github.com/tensorflow/hub/raw/master/examples/image_retraining/retrain.py
python retrain.py --image_dir ~/flower_photos

訓練完成後,將開始測試此模型

curl -LO https://github.com/tensorflow/tensorflow/raw/master/tensorflow/examples/label_image/label_image.py
python label_image.py \
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
--input_layer=Placeholder \
--output_layer=final_result \
--image=~/flower_photos/daisy/21652746_cc379e0eea_m.jpg

使用openvino測試模型,首先先將tensorflow訓練好的模型pb轉換成openvino使用的格式

cd /tmp
~/intel/openvino/deployment_tools/model_optimizer/mo_tf.py --input_model output_graph.pb --batch 1 --data_type FP16

基於openvino模型使用

from openvino.inference_engine import IENetwork, IEPlugin
import numpy as np
import cv2
import sys
import time
import os

def load_labels():
    labels = []
    with open('/tmp/output_labels.txt') as f:
        for i in f.readlines():
            labels.append(i.replace("\n", ""))
    return labels

if __name__ == '__main__':
    labels = load_labels()
    plugin = IEPlugin(device="MYRIAD")
    net = IENetwork(model="/tmp/output_graph.xml", weights="/tmp/output_graph.bin")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    exec_net = plugin.load(network=net)
    img = cv2.imread('~/flower_photos/daisy/21652746_cc379e0eea_m.jpg')
    img = img[:,:,::-1]
    img = cv2.resize(img, (299,299), cv2.INTER_CUBIC)
    img = img/255.0
    img = img.transpose((2, 0, 1))
    img = img.reshape(1, 3, 299, 299)
    req_handle = exec_net.start_async(request_id=0,inputs={input_blob:img})
    status = req_handle.wait()
    res = req_handle.outputs[out_blob]
    res = np.squeeze(res)
    top_inds = res.argsort()[-5:][::-1]
    for i in top_inds:
        print(labels[i], res[i])

辨識結果與tensorflow辨識結果相似


上一篇
[Day 28] OpenVino安裝
下一篇
[Day 30] Jetson Nano 從安裝到模型應用
系列文
從開源雲到邊緣運算30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言